#include "rtthread.h"
#include "fun.h"
#include <rtdevice.h>
#include <string.h>
#include "include.h"
#include "msh.h"
#include "player.h"
#include "ntp.h"
#include "hdc1080.h"
//#include "drv_spi.h"
#include "drv_lcd.h"
//#include "drv_lcd_font.h"
//#include "rttlogo.h"


#define LED_THREAD_PRIORITY     5
#define LED_THREAD_TIMESLICE    5
#define LED_THREAD_STACK_SIZE   (512*8)

#define LCD_THREAD_PRIORITY     6
#define LCD_THREAD_TIMESLICE    5
#define LCD_THREAD_STACK_SIZE   (1024*8)

#define MQ_2_THREAD_PRIORITY     7
#define MQ_2_THREAD_TIMESLICE    5
#define MQ_2_THREAD_STACK_SIZE   (128*8)

struct rt_semaphore mq_2_sem;
//struct rt_semaphore mq_2_sem_clear;

struct rt_mailbox mq2_alarm;
char mb_pool[10];
static char mb_open[]="alarm open";
static char mb_close[]="alarm close";


static void lcd_thread_entry(void *parameter)
{
    unsigned char *str;
    unsigned char alarm_open_flag = 0;
    unsigned char alarm_close_flag = 0;
    lcd_clear(BLACK);

    while(1)
    {
        //rt_sem_take(&mq_2_sem, RT_WAITING_FOREVER);
        if(rt_mb_recv(&mq2_alarm, (rt_uint32_t*)&str, RT_WAITING_FOREVER) == RT_EOK)
        {
            //rt_kprintf("str== %s",str);
        }
        if (strcmp(str, "alarm open") == 0)
        {
            if(alarm_open_flag==1)
            {
                alarm_open_flag=0;
                lcd_clear(RED); 
            }
            
            lcd_set_color(RED, WHITE);
            lcd_show_string(16, 105, 32, "MQ_2 ALARM !");
            
            alarm_close_flag=0;
        }
        else if(strcmp(str, "alarm close") == 0)
        {
            if(alarm_close_flag == 0)
            {
                alarm_close_flag=1;
                lcd_clear(BLACK);
            }
             
            lcd_set_color(BLACK, WHITE);
            lcd_show_string(16, 105, 32, "MQ_2 NORMAL !");
           
            alarm_open_flag = 1;
        }
        
        rt_thread_delay(500);
    }
}



static void mq_2_thread_entry(void *parameter)
{
    while(1)
    {
        if(rt_pin_read(MQ_2_PIN_NUM) == PIN_LOW)
        {
            rt_thread_delay(10);
            if(rt_pin_read(MQ_2_PIN_NUM) == PIN_LOW)
            {
                rt_sem_release(&mq_2_sem);   //释放信号量
                rt_mb_send(&mq2_alarm, (rt_uint32_t)&mb_open[0]);
            }
        }
        else
        {
            rt_mb_send(&mq2_alarm,(rt_uint32_t)&mb_close[0]);
        }
        
        rt_thread_delay(1000);

    }

}

static void led_thread_entry(void *parameter)
{
    player_set_volume(90);
    while(1)
    {
        rt_sem_take(&mq_2_sem, RT_WAITING_FOREVER);
        player_stop();
        player_set_uri("sd/Classic/msg.wav");
        player_play();

	    rt_pin_write(LED1_PIN_NUM, PIN_HIGH);
        rt_pin_write(LED2_PIN_NUM, PIN_LOW);
        rt_thread_mdelay(200);
        rt_pin_write(LED1_PIN_NUM, PIN_LOW);
        rt_pin_write(LED2_PIN_NUM, PIN_HIGH);
        rt_thread_mdelay(200);
        rt_pin_write(LED1_PIN_NUM, PIN_HIGH);
        rt_pin_write(LED2_PIN_NUM, PIN_LOW);
        rt_thread_mdelay(200);
        rt_pin_write(LED1_PIN_NUM, PIN_LOW);
        rt_pin_write(LED2_PIN_NUM, PIN_HIGH);
        rt_thread_mdelay(200);
    }
}

int fun_app_start()
{
    rt_pin_mode(MQ_2_PIN_NUM, PIN_MODE_INPUT_PULLUP);
    rt_pin_mode(LED1_PIN_NUM, PIN_MODE_OUTPUT);
    rt_pin_mode(LED2_PIN_NUM, PIN_MODE_OUTPUT);

    rt_pin_write(LED1_PIN_NUM, PIN_HIGH);
    rt_pin_write(LED2_PIN_NUM, PIN_HIGH);


    rt_sem_init(&mq_2_sem, "mq_2_sem",   0,    RT_IPC_FLAG_FIFO);//初始化信号量

    rt_mb_init(
                &mq2_alarm,
                "mq2_alarm",
                &mb_pool[0],
                sizeof(mb_pool)/4,
                RT_IPC_FLAG_FIFO);
    
    //创建线程1
    rt_thread_t led_tid= rt_thread_create("led",
                                    led_thread_entry, RT_NULL,
                                    LED_THREAD_STACK_SIZE,
                                    LED_THREAD_PRIORITY,LED_THREAD_TIMESLICE);
    if (led_tid != RT_NULL)
        {
            rt_thread_startup(led_tid);
        }
    else
    {
        rt_kprintf("create led thread failed...\r\n");
    }
    rt_kprintf("create led thread \r\n");

    //创建线程2
    rt_thread_t lcd_tid = rt_thread_create("spilcd",
                                lcd_thread_entry, RT_NULL,
                                LCD_THREAD_STACK_SIZE,
                                LCD_THREAD_PRIORITY, 
                                LCD_THREAD_TIMESLICE);

    /* 如果获得线程控制块，启动这个线程 */
    if (lcd_tid  != RT_NULL)
    {
        rt_thread_startup(lcd_tid );
    }

    //创建线程3
    rt_thread_t mq_2_tid = rt_thread_create("mq-2",
                                mq_2_thread_entry, RT_NULL,
                                MQ_2_THREAD_STACK_SIZE,
                                MQ_2_THREAD_PRIORITY, 
                                MQ_2_THREAD_TIMESLICE);
/* 如果获得线程控制块，启动这个线程 */
if (mq_2_tid  != RT_NULL)
{
    rt_thread_startup(mq_2_tid );
}
     return 0;
}